#include "order.h"
#include <QDebug>

Order::Order()
    : m_orderId(0),
      m_userId(0),
      m_customerId(0),
      m_totalAmount(0.0),
      m_discountAmount(0.0),
      m_finalAmount(0.0),
      m_paymentMethod(PAYMENT_CASH),
      m_status(ORDER_PENDING)
{
    m_orderTime = QDateTime::currentDateTime();
}

Order::Order(int userId, int customerId, double totalAmount,
             double discountAmount, double finalAmount,
             PaymentMethod method)
    : m_orderId(0),
      m_userId(userId),
      m_customerId(customerId),
      m_totalAmount(totalAmount),
      m_discountAmount(discountAmount),
      m_finalAmount(finalAmount),
      m_paymentMethod(method),
      m_status(ORDER_PENDING)
{
    m_orderTime = QDateTime::currentDateTime();
}

Order::Order(int orderId, int userId, int customerId,
             const QDateTime &orderTime, double totalAmount,
             double discountAmount, double finalAmount,
             PaymentMethod method, const QString &transactionNo,
             const QDateTime &paymentTime, OrderStatus status)
    : m_orderId(orderId),
      m_userId(userId),
      m_customerId(customerId),
      m_orderTime(orderTime),
      m_totalAmount(totalAmount),
      m_discountAmount(discountAmount),
      m_finalAmount(finalAmount),
      m_paymentMethod(method),
      m_transactionNo(transactionNo),
      m_paymentTime(paymentTime),
      m_status(status)
{
}

int Order::getOrderId() const
{
    return m_orderId;
}

int Order::getUserId() const
{
    return m_userId;
}

int Order::getCustomerId() const
{
    return m_customerId;
}

QDateTime Order::getOrderTime() const
{
    return m_orderTime;
}

double Order::getTotalAmount() const
{
    return m_totalAmount;
}

double Order::getDiscountAmount() const
{
    return m_discountAmount;
}

double Order::getFinalAmount() const
{
    return m_finalAmount;
}

PaymentMethod Order::getPaymentMethod() const
{
    return m_paymentMethod;
}

QString Order::getTransactionNo() const
{
    return m_transactionNo;
}

QDateTime Order::getPaymentTime() const
{
    return m_paymentTime;
}

OrderStatus Order::getStatus() const
{
    return m_status;
}

QVector<OrderDetail> Order::getOrderDetails() const
{
    return m_orderDetails;
}

void Order::setOrderId(int orderId)
{
    m_orderId = orderId;
}

void Order::setTransactionNo(const QString &transactionNo)
{
    m_transactionNo = transactionNo;
}

void Order::setPaymentTime(const QDateTime &paymentTime)
{
    m_paymentTime = paymentTime;
}

void Order::setStatus(OrderStatus status)
{
    m_status = status;
}

void Order::addOrderDetail(int productId, int quantity,
                          double unitPrice, double subtotal,
                          bool isDiscounted)
{
    OrderDetail detail;
    detail.orderId = m_orderId;
    detail.productId = productId;
    detail.quantity = quantity;
    detail.unitPrice = unitPrice;
    detail.subtotal = subtotal;
    detail.isDiscounted = isDiscounted;

    m_orderDetails.append(detail);
}

Order Order::fromDatabaseResult(const QMap<QString, QVariant> &result)
{
    int orderId = result["order_id"].toInt();
    int userId = result["user_id"].toInt();
    int customerId = result["customer_id"].isNull() ? 0 : result["customer_id"].toInt();
    QDateTime orderTime = result["order_time"].toDateTime();
    double totalAmount = result["total_amount"].toDouble();
    double discountAmount = result["discount_amount"].toDouble();
    double finalAmount = result["final_amount"].toDouble();
    PaymentMethod method = static_cast<PaymentMethod>(result["method_id"].toInt());
    QString transactionNo = result["transaction_no"].toString();
    QDateTime paymentTime = result["payment_time"].toDateTime();
    OrderStatus status = static_cast<OrderStatus>(result["order_status"].toInt());

    return Order(orderId, userId, customerId, orderTime, totalAmount,
                discountAmount, finalAmount, method, transactionNo,
                paymentTime, status);
}

QMap<QString, QVariant> Order::toDatabaseFormat() const
{
    QMap<QString, QVariant> data;
    data["order_id"] = m_orderId;
    data["user_id"] = m_userId;
    data["customer_id"] = m_customerId > 0 ? m_customerId : QVariant(); // 非会员订单设为NULL
    data["order_time"] = m_orderTime;
    data["total_amount"] = m_totalAmount;
    data["discount_amount"] = m_discountAmount;
    data["final_amount"] = m_finalAmount;
    data["method_id"] = static_cast<int>(m_paymentMethod);
    data["transaction_no"] = m_transactionNo;
    data["payment_time"] = m_paymentTime;
    data["order_status"] = static_cast<int>(m_status);

    return data;
}
